package com.jason.kotlin.meta.architecture2.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import cn.bingoogolapple.bgabanner.BGABanner
import com.jason.kotlin.meta.R
import com.jason.kotlin.meta.architecture2.base.WanBaseFragment
import com.jason.kotlin.meta.architecture2.model.ArticleBean
import com.jason.kotlin.meta.architecture2.ui.LoginActivity
import com.jason.kotlin.meta.architecture2.ui.adapter.BannerAdapter
import com.jason.kotlin.meta.architecture2.ui.adapter.HomeArticleAdapter
import com.jason.kotlin.meta.architecture2.viewmodel.HomeViewModel
import com.jason.library.common.base.Status
import com.jason.library.common.ext.getColor
import com.jason.library.common.ext.observe
import com.jason.library.common.ext.showToast
import com.jason.library.common.ext.start

class HomeFragment : WanBaseFragment() {

    private lateinit var toolbar: Toolbar
    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView

    private lateinit var banner: BGABanner

    private lateinit var homeArticleAdapter: HomeArticleAdapter
    private lateinit var bannerAdapter: BannerAdapter

    private lateinit var viewModel: HomeViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)//加上这句话，menu才会显示出来
    }

    override fun getLayoutId() = R.layout.fragment_home

    override fun onViewInit(view: View) {
        toolbar = view.findViewById(R.id.toolbar)
        toolbar.run {
            setBackgroundColor(getColor(R.color.colorAccent))
            title = "首页"
            inflateMenu(R.menu.home_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.home_search -> start(LoginActivity::class.java)
                }
                true
            }
        }

        refreshLayout = view.findViewById(R.id.swipeRefreshLayout)
        recyclerView = view.findViewById(R.id.recyclerView)
        //获取banner页面，并赋值,设置监听
        val view = LayoutInflater.from(activity).inflate(R.layout.include_banner, null)
        banner = view.findViewById(R.id.banner)

        homeArticleAdapter = HomeArticleAdapter(R.layout.item_article)
        bannerAdapter = BannerAdapter(activity!!)

        banner.setAdapter(bannerAdapter)
        banner.setDelegate { banner, itemView, model, position -> showToast("当前功能，开发中......") }

        homeArticleAdapter.addHeaderView(view)

        homeArticleAdapter.loadMoreModule.setOnLoadMoreListener {
            loadMoreData()
        }
        homeArticleAdapter.loadMoreModule.isEnableLoadMore = false
        homeArticleAdapter.loadMoreModule.isAutoLoadMore = true
        homeArticleAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false

        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            setHasFixedSize(true)
            adapter = homeArticleAdapter
        }

        refreshLayout.setOnRefreshListener {
            loadData()
        }

    }

    override fun onDataInit() {

        viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(activity!!.application).create(
            HomeViewModel::class.java
        )
        bindLiveData()

        loadData()
    }

    private fun loadData() {
        homeArticleAdapter.loadMoreModule.isEnableLoadMore = false
        refreshLayout.isRefreshing = true
        viewModel.loadData()
    }

    private fun loadMoreData() {
        homeArticleAdapter.loadMoreModule.isEnableLoadMore = false
        refreshLayout.isRefreshing = true
        viewModel.loadMoreData()
    }

    /**
     * 绑定LiveData的监听
     */
    private fun bindLiveData() {

        observe(viewModel.pageStatusData) {
            when (it.status) {
                Status.SUCCESS -> {
                    refreshLayout.isRefreshing = false
                    homeArticleAdapter.loadMoreModule.isEnableLoadMore = true
                }
                Status.EMPTY -> showToast("加载不到数据")
                Status.ERROR -> showToast("加载失败")
                Status.LOADING -> refreshLayout.isRefreshing = true
            }
        }

        //banner 数据监听
        observe(viewModel.bannerData) {
            banner.setData(it.data, null)
        }

        observe(viewModel.articleData) {
            if (it.page > 0) {
                homeArticleAdapter.setNewData(it.data as MutableList<ArticleBean>?)
            } else {
                homeArticleAdapter.addData(it.data!!)
            }


        }


    }
}